/*

File name		: figure3a.do

Description		: Donation rates by past donation
									
Output File		: figure3a.png
				
*/



set more off, perm
capture restore

*** 001 Plot Descriptive Graph ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"

	// Rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort spnr entnahmedatum
	gen any_rain_plus1 = any_rain[_n+1] if spnr == spnr[_n+1]

	sort spnr period
	sum don if period == 1
	sum don if period == 2 & don[_n-1] == 1 & spnr == spnr[_n-1]
	sum don if period == 2 & don[_n-1] == 0 & spnr == spnr[_n-1]
	sum don if period == 3 & don[_n-1] == 1 & spnr == spnr[_n-1]
	sum don if period == 3 & don[_n-1] == 0 & spnr == spnr[_n-1]
	sum don if period == 4 & don[_n-1] == 1 & spnr == spnr[_n-1]
	sum don if period == 4 & don[_n-1] == 0 & spnr == spnr[_n-1]

	/* Gen new variable that is 1=donated in period 1, 2=did not donate in period 1, 
	  3=donated in period2, 4=did not donate in period 2 */
	sort spnr period
	gen treatment_bydonation1=1 if period == 1 & don == 1
	replace treatment_bydonation1=0 if period == 1 & don == 0
	replace treatment_bydonation1=treatment_bydonation1[_n-1] if spnr == spnr[_n-1]
	gen treatment_bydonation2=1 if period == 2 & don == 1
	replace treatment_bydonation2=0 if period == 2 & don == 0
	replace treatment_bydonation2=treatment_bydonation2[_n-1] ///
	if spnr == spnr[_n-1] & treatment_bydonation2[_n-1]!=.
	replace treatment_bydonation2=treatment_bydonation2[_n+1] ///
	if spnr == spnr[_n+1] & treatment_bydonation2[_n+1]!=.

	preserve
	
		collapse (mean) meandon = don (sd) sddon=don (count) n=don, ///
		by (treatment_bydonation1 period)
		
	save "data/newgraph_temp.dta", replace
	
	restore

	collapse (mean) meandon = don (sd) sddon=don (count) n=don, by (treatment_bydonation2 period )
	append using "data/newgraph_temp.dta"
	
	gen treatment_bydonation=treatment_bydonation1+1 if treatment_bydonation1!=.
	replace treatment_bydonation=treatment_bydonation2+3 if treatment_bydonation2!=.
	sort period treatment_bydonation 
	replace meandon=(n*meandon + n[_n+1]*meandon[_n+1])/(n+n[_n+1]) if period == 1 & treatment_bydonation == 1
	replace meandon=meandon[_n-1] if period == 1 & treatment_bydonation == 2
	replace sddon=sqrt(meandon*(1-meandon)) if period == 1 & treatment_bydonation == 1
	replace sddon=sddon[_n-1] if period == 1 & treatment_bydonation == 2
	replace meandon=(n*meandon + n[_n+1]*meandon[_n+1])/(n+n[_n+1]) if period == 2 & treatment_bydonation == 3
	replace meandon=meandon[_n-1] if period == 2 & treatment_bydonation == 4
	replace sddon=sqrt(meandon*(1-meandon)) if period == 2 & treatment_bydonation == 3
	replace sddon=sddon[_n-1] if period == 2 & treatment_bydonation == 4
	
	
	// SE
	generate hidon = meandon + (sddon / sqrt(n))
	generate lowdon = meandon - (sddon / sqrt(n))

	// Fix n for combined scatters
	replace hidon = meandon + (sddon / sqrt(1400)) ///
	if period == 1 & (treatment_bydonation == 1|treatment_bydonation == 2)
	replace lowdon = meandon - (sddon / sqrt(1400)) ///
	if period == 1 & (treatment_bydonation == 1|treatment_bydonation == 2)
	replace hidon = meandon + (sddon / sqrt(1400)) ///
	if period == 2 & (treatment_bydonation == 3|treatment_bydonation == 4)
	replace lowdon = meandon - (sddon / sqrt(1400)) ///
	if period == 2 & (treatment_bydonation == 3|treatment_bydonation == 4)

	generate periodtreatment = treatment_bydonation  if period == 1
	replace periodtreatment = treatment_bydonation+5 if period == 2
	replace periodtreatment = treatment_bydonation+10 if period == 3
	replace periodtreatment = treatment_bydonation+15 if period == 4

	sort periodtreatment
	list periodtreatment period treatment_bydonation, sepby(period)

	generate periodtreatment2 = 1.1 if period == 1
	replace periodtreatment2 = treatment_bydonation*0.05+2 if period == 2
	replace periodtreatment2 = treatment_bydonation*0.05+3 if period == 3
	replace periodtreatment2 = treatment_bydonation*0.05+4 if period == 4

	/* Omit period 1 for condition of donation in period 1 and period 2 
	  for condition of donation in period 2 */
	replace periodtreatment2 = . if periodtreatment < 3
	replace periodtreatment2 = . if periodtreatment == 8 | periodtreatment == 9
	
	// Line graph
	local i = 0
	local ml "S O S O"
	local l "solid solid dash dash"
	local gs "black black gs10 gs10"
	
	forvalues i = 1(1)4 {
		
		local gr `gr' rcap hidon lowdon periodtreatment2 ///
		if treatment_bydonation == `i', lcolor("`: word `i' of `gs''") ///
		|| connected meandon periodtreatment2 if treatment_bydonation == `i', ///
		cmissing(n) lpattern("`: word `i' of `l''") lwidth(thin) mfcolor(white) ///
		msize(vlarge) msymbol("`: word `i' of `ml''") ///
		mcolor("`: word `i' of `gs''") lcolor("`: word `i' of `gs''")||
		
		*graph twoway `gr' , legend(nobox region(style(none)) pos(2) ring(0) ///
		col(1) order(2 "Control" 4 "Treatment C1" 6 "Treatment C2" 8 "Treatment C12")) ///
		graphregion(color(white)) bgcolor(white) ylabel(0.1(0.05)0.3) xtitle("") ///
		ytitle("Donation rate") xscale(range(0.8 4.4)) xlabel(1.1 "Period 1" 2.1 ///
		"Period 2" 3.1 "Period 3" 4.1 "Period 4", noticks)
		
		
		}
		
	graph twoway `gr' , legend(nobox region(style(none)) size(medium) pos(2) ring(0) col(1) ///
	order(4 "Donation in P1" 2 "No donation in P1" ///
	8 "Donation in P2" 6 "No donation in P2")) ///
	graphregion(color(white)) bgcolor(white) ylabel(0.1(0.1)0.5, labsize(medium)) xtitle("") ///
	ytitle("Donation rate", size(medium)) xscale(range(0.8 4.4)) xlabel(1.1 "Period 1" ///
	2.1 "Period 2" 3.1 "Period 3" 4.1 "Period 4", noticks labsize(medium))
	
	*order(4 "Donation in P1 (N=251)" 2 "No donation in P1 (N=1149)" ///
	8 "Donation in P2 (N=293)" 6 "No donation in P2 (N=1107)")) ///
	
	graph export output/figures/figure3a.png, replace width(2048)
	
	rm data/newgraph_temp.dta
	
